const openIdUrl = require('./config').openIdUrl

App({
  onLaunch: function () {
    console.log('App Launch')
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  },
  globalData: {
    hasLogin: false,
    openid: null,
    // host:'http://adjx.com/index.php?key=123456'
    // host:'https://xiaochengxu.xm-ttyc.com/index.php?key=123456'
    host: 'https://xiaochengxu.xm-ttyc.com/index.php?g=App&key=123456',
    scene:"",
    uid:0,
    userInfo:"",
    isLogin:false
  },
  // lazy loading openid
  getUserOpenId: function (getUid, getInfo) {
    var that = this
    if (that.globalData.isLogin){
      console.log("已登录")
      getUid(that.globalData.uid)
      getInfo(that.globalData.userInfo)
    }else{
      wx.getStorage({
        key: 'uid',
        success: function (res) {
          if (res.data) {
            console.log("有值"+res.data)
            that.globalData.uid = res.data
            that.globalData.isLogin = true
            getUid(res.data)
            wx.getUserInfo({
              success: function (res) {
                that.globalData.userInfo = res.userInfo
                getInfo(res.userInfo)
              }
            })
          } else {
            console.log("无值")
            that.wxLogin(getUid, getInfo)
          }
        },
        fail: function () {
          console.log("取出失败")
          that.wxLogin(getUid, getInfo)
        },
      })
    }

  },

  wxLogin: function (getUid,getInfo) {
    var that = this
    console.log("微信登录")
    wx.login({
      success: function (res) {
        console.log("微信登录成功")
        if (res.code) {
          var code = res.code
          console.log("有code" + code)
          wx.request({
            url: that.globalData.host + "&m=Index&a=login&si=1", //仅为示例，并非真实的接口地址
            data: {
              "code": code,
            },
            header: {
              'content-type': 'application/json', // 默认值
            },
            method: 'GET',
            success: function (res) {
              console.log("驾校")
              console.log(res.data)
              var sessionKey = res.data.session_key
              wx.getUserInfo({
                success: function (res) {
                  that.globalData.userInfo = res.userInfo
                  getInfo(res.userInfo)
                  wx.request({
                    url: that.globalData.host + "&m=Index&a=userinfo&si=1", //仅为示例，并非真实的接口地址
                    data: {
                      "sessionKey": sessionKey,
                      "encryptedData": res.encryptedData,
                      "iv": res.iv,
                      "scene": that.globalData.scene,
                    },
                    header: {
                      'content-type': 'application/json', // 默认值
                    },
                    method: 'GET',
                    success: function (res) {
                      console.log("驾校11")
                      console.log(res.data)
                      that.globalData.uid = res.data
                      that.globalData.isLogin = true
                      wx.setStorage({
                        key: 'uid',
                        data: res.data,
                        success: function () {

                        }
                      })
                      getUid(res.data)
                    },
                    fail: function (res) {
                      wx.showToast({
                        title: '网络请求失败',
                      })
                    }
                  })
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '网络请求失败',
              })
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
    });
  }
})
